#include "MeshLoader.hpp"

#include "boost/algorithm/string.hpp"
#include "GtsFile.hpp"
#include "MeshView.hpp"
#include "ObjFile.hpp"
#include "OffFile.hpp"
#include "PolFile.hpp"
#include "SmeshFile.hpp"
#include "SufFile.hpp"
#include "TetgenFile.hpp"
#include "DebugHelper.hpp"
#include "SubFile.hpp"
namespace MeshLoader
{

bool Load(MeshView&   aView,
          std::string aFileName)
{
   DEBUG_LOG("MeshLoader::Load")
   bool loaded=true;
   bool polymesh=false;
   std::vector<std::string> parts;
   aView.mMesh.Clear();
   aView.mName = boost::split(parts, aFileName, boost::is_any_of("."))[0];
   aView.mName = boost::split(parts, aView.mName, boost::is_any_of("/\\")).back();
   if (boost::ends_with(aFileName, ".suf"))
   {
      SufFile::Read(aFileName, aView.mMesh.GetCreaseMesh());
   }
   else if (boost::ends_with(aFileName, ".obj"))
   {
      ObjFile::Read(aFileName, aView.mMesh.GetCreaseMesh());
   }
   else if (boost::ends_with(aFileName, ".gts"))
   {
      GtsFile::Read(aFileName, aView.mMesh.GetCreaseMesh());
   }
   else if (boost::ends_with(aFileName, ".off"))
   {
      OffFile::Read(aFileName, aView.mMesh.GetCreaseMesh());
   }
   else if (boost::ends_with(aFileName, ".smesh"))
   {
      SmeshFile::Read(aFileName, aView.mMesh.GetCreaseMesh());
   }
   else if (boost::ends_with(aFileName, ".node"))
   {
      polymesh=true;
      TetgenFile::Read(aFileName, aView.mMesh);
   }
   else if (boost::ends_with(aFileName, ".pol"))
   {
      polymesh=true;
      PolFile::Read(aFileName, aView.mMesh);
   }
   else if (boost::ends_with(aFileName, ".sub"))
   {
      polymesh=true;
      loaded = SubFile::Read(aFileName, aView.mMesh);
   }
   else
   {
      loaded=false;
   }
   if (loaded)
   {
      if (polymesh)
      {
         aView.mMesh.UpdatePolys();
      }
      else
      {
         aView.mMesh.UpdateFaces();
      }
      aView.UpdateMesh();
   }
   DEBUG_LOG(".\n")
   return loaded;

}

}